﻿using System;
using System.IO.Ports;
using System.Text;

namespace ConsoleApp3
{
    class PortSetting
    {
        #region 变量
        public SerialPort port = null;

        private string portName;
        private int baudRate;
        private Parity parity;
        private StopBits stopBits;
        private int dataBits;
        private int readBufferSize;
        private int readTimeOut;
        #endregion

        public PortSetting()
        {
            port = new SerialPort();
        }

        public PortSetting(string portName, int baudRate, Parity parity, StopBits stopBits, int dataBits, int readBufferSize, int readTimeOut)
        {
            if (port == null)
            {
                port = new SerialPort();
            }
            this.portName = portName;
            this.baudRate = baudRate;
            this.parity = parity;
            this.stopBits = stopBits;
            this.dataBits = dataBits;
            this.readBufferSize = readBufferSize;
            this.readTimeOut = readTimeOut;
        }

        public bool setPort()
        {
            if (port == null)
            {
                port = new SerialPort();
            }
            port.PortName = portName;
            port.BaudRate = baudRate;
            port.Parity = parity;
            port.StopBits = stopBits;
            port.DataBits = dataBits;
            port.ReadBufferSize = readBufferSize;
            port.ReadTimeout = readTimeOut;
            try
            {
                port.Open();
                if (port.IsOpen)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool setPort(int baudRate, Parity parity, StopBits stopBits, int dataBits, int readBufferSize, int readTimeOut)
        {
            string[] portNames = SerialPort.GetPortNames();

            if (portNames.Length > 0)
            {
                if (port == null)
                {
                    port = new SerialPort();
                }
                port.BaudRate = baudRate;
                port.Parity = parity;
                port.StopBits = stopBits;
                port.DataBits = dataBits;
                port.ReadBufferSize = readBufferSize;
                port.ReadTimeout = readTimeOut;
                foreach (string portName in portNames)
                {
                    port.PortName = portName;
                    try
                    {
                        port.Open();
                        if (port.IsOpen)
                        {
                            return true;
                        }
                    }
                    catch
                    { }
                }
                return false;
            }
            else
            {
                return false;
            }
        }

        public bool setPort(string portName, int baudRate, Parity parity, StopBits stopBits, int dataBits, int readBufferSize, int readTimeOut)
        {
            if (port == null)
            {
                port = new SerialPort();
            }
            port.PortName = portName;
            port.BaudRate = baudRate;
            port.Parity = parity;
            port.StopBits = stopBits;
            port.DataBits = dataBits;
            port.ReadBufferSize = readBufferSize;
            port.ReadTimeout = readTimeOut;
            try
            {
                port.Open();
                if (port.IsOpen)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool writePort(string writeString)
        {
            try
            {
                System.Text.UTF8Encoding utf8 = new UTF8Encoding();
                Byte[] writeBytes = utf8.GetBytes(writeString);
                port.Write(writeBytes, 0, writeBytes.Length);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string readPortByte()
        {
            try
            {
                string readString = string.Empty;
                int DataCount = port.BytesToRead;
                byte[] readBytes = new byte[DataCount];
                port.Read(readBytes, 0, DataCount);
                System.Text.UTF8Encoding utf8 = new UTF8Encoding();
                readString += utf8.GetString(readBytes);
                return readString;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public string readPortByte(string beginString, string endString)
        {
            try
            {
                string readString = string.Empty;
                int DataCount = port.BytesToRead;
                byte[] readBytes = new byte[DataCount];
                port.Read(readBytes, 0, DataCount);
                System.Text.UTF8Encoding utf8 = new UTF8Encoding();
                readString = utf8.GetString(readBytes);
                if (readString.IndexOf(beginString) >= 0 && readString.IndexOf(endString) >= 0)
                {
                    readString = readString.Substring(readString.IndexOf(beginString) + beginString.Length, readString.IndexOf(endString) - readString.IndexOf(beginString) - beginString.Length);
                }
                return readString;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public string readPortString(string beginString, string endString)
        {
            try
            {
                string readString = string.Empty;
                port.ReadTo(beginString);
                readString = port.ReadTo(endString);
                string temp = port.ReadExisting();
                return readString;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public bool closePort()
        {
            try
            {
                if (port.IsOpen)
                {
                    port.Dispose();
                    port.Close();
                    port = null;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        ~PortSetting()
        {
            if (closePort())
            {
                port = null;
            }
        }
    }
}
